package oa.hleast.xswl.service.studies.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.utils.JsonUtil;
import oa.hleast.xswl.mapper.studies.ResearchActivitiesMapper;
import oa.hleast.xswl.mapper.studies.ResearchActivitiesPathMapper;
import oa.hleast.xswl.mapper.studies.ResearchActivitiesPointMapper;
import oa.hleast.xswl.pojo.domain.studies.ResearchActivities;
import oa.hleast.xswl.pojo.domain.studies.ResearchActivitiesPath;
import oa.hleast.xswl.pojo.domain.studies.ResearchActivitiesPoint;
import oa.hleast.xswl.pojo.vo.studies.ResearchActivitesPointVo;
import oa.hleast.xswl.service.studies.service.ResearchActivitiesPathService;
import oa.hleast.xswl.service.studies.service.ResearchActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 研学活动表(ResearchActivities)表服务实现类
 *
 * @author makejava
 * @since 2021-11-08 13:35:49
 */
@AllArgsConstructor
@Transactional
@Slf4j
@Service("researchActivitiesService")
public class ResearchActivitiesServiceImpl extends ServiceImpl<ResearchActivitiesMapper, ResearchActivities> implements ResearchActivitiesService {


    private final ResearchActivitiesPathMapper researchActivitiesPathDao;


    private final ResearchActivitiesPathService researchActivitiesPathService;


    private final ResearchActivitiesPointMapper researchActivitiesPointDao;

    @Override
    public boolean addStudiesPoint(Integer researchActivitiesPathId,String ids) {
        if(researchActivitiesPathId==null&&ids==null){
            return false;
        }

        ResearchActivitiesPath researchActivitiesPath = researchActivitiesPathDao.selectOne(new LambdaQueryWrapper<ResearchActivitiesPath>().eq(ResearchActivitiesPath::getId, researchActivitiesPathId));
        //固定线路点位集合
        ArrayList<ResearchActivitesPointVo> researchActivitesPointVoList = JsonUtil.string2Collection(researchActivitiesPath.getCmsPlaceInfoIdCollection(), new TypeReference<ArrayList<ResearchActivitesPointVo>>() {
        });
        //新添加的点位集合
        List<String> newPointList = Arrays.asList(ids.split(","));
        for (ResearchActivitesPointVo researchActivitesPointVo : researchActivitesPointVoList) {
            newPointList.add(researchActivitesPointVo.getId().toString());
        }

        for (String newPoint : newPointList) {
            ResearchActivitiesPoint researchActivitiesPoint=new ResearchActivitiesPoint();
            researchActivitiesPoint.setResearchActivitiesId(Long.valueOf(researchActivitiesPathId));
            researchActivitiesPoint.setCmsPlaceInfoId(Long.valueOf(newPoint));
            researchActivitiesPoint.setCertification(1);
            int insert = researchActivitiesPointDao.insert(researchActivitiesPoint);

            if(insert!=1){
                return false;
            }
        }
        return true;
    }
}

